package com.hainiu.cat.interview.juc;

import lombok.Data;

/**
 * create by biji.zhao on 2021/7/2
 */
public class TestVolatile {
    public static void main(String[] args){ //这个线程是用来读取flag的值的
        ThreadDemo threadDemo = new ThreadDemo();
        Thread thread = new Thread(threadDemo);
        thread.start();
        while (true){
            if (threadDemo.isAaaaaa()){
                System.out.println("主线程读取到的flag = " + threadDemo.isAaaaaa());
                break;
            }
        }
    }
}

@Data
class ThreadDemo implements Runnable{ //这个线程是用来修改flag的值的
    public boolean aaaaaa = false;
    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        aaaaaa = true;
        System.out.println("ThreadDemo线程修改后的flag = " + aaaaaa);
    }
}